package org.queryexecutor.swing.dialogs;

import java.awt.BorderLayout;
import java.awt.Frame;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JSeparator;
import java.awt.CardLayout;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import org.queryexecutor.database.drivers.DatabaseDrivers;
import org.queryexecutor.swing.frames.components.methods.ButtonMethods;
import org.queryexecutor.swing.frames.components.methods.ComboBoxMethods;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JLayeredPane;
import org.queryexecutor.swing.panels.ServerPanel;
import org.queryexecutor.swing.panels.LocalFilePanel;

public class DatabaseLogin extends JDialog
{
	
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	private final JPanel	contentPanel	= new JPanel();
	
	private ComboBoxMethods cbMethods;
	private ButtonMethods bMethods;
	
	private static DatabaseLogin dbLogin;
	private JComboBox dbTypesCB;
	private JPanel centerPane;
	private LocalFilePanel localFilePane;
	private ServerPanel serverPane;
	
	public JComboBox getDbTypesCB()
	{
		return this.dbTypesCB;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		try {
			DatabaseLogin dialog = new DatabaseLogin(null);
			dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void instantiateMethods()
	{
		this.cbMethods = ComboBoxMethods.getInstance();
		this.bMethods = ButtonMethods.getInstance();
	}
	
	public static DatabaseLogin getInstance(Frame owner)
	{
		if(dbLogin == null){
			dbLogin = new DatabaseLogin(owner);
		}
		return dbLogin;
	}
	
	/**
	 * Create the dialog.
	 */
	private DatabaseLogin(Frame owner)
	{
		super(owner);
		setModalityType(ModalityType.DOCUMENT_MODAL);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setModal(true);
		setResizable(false);
		setTitle("Database");
		setBounds(100, 100, 369, 230);
		getContentPane().setLayout(new BorderLayout());
		this.contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(this.contentPanel, BorderLayout.CENTER);
		this.contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel selectPane = new JPanel();
			this.contentPanel.add(selectPane, BorderLayout.NORTH);
			GridBagLayout gbl_selectPane = new GridBagLayout();
			gbl_selectPane.columnWidths = new int[]{83, 77, 0};
			gbl_selectPane.rowHeights = new int[]{14, 0, 0};
			gbl_selectPane.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
			gbl_selectPane.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
			selectPane.setLayout(gbl_selectPane);
			{
				JLabel label = new JLabel("Database Type:");
				GridBagConstraints gbc_label = new GridBagConstraints();
				gbc_label.fill = GridBagConstraints.BOTH;
				gbc_label.insets = new Insets(0, 0, 5, 5);
				gbc_label.gridx = 0;
				gbc_label.gridy = 0;
				selectPane.add(label, gbc_label);
			}
			{
				this.dbTypesCB = new JComboBox();
				this.dbTypesCB.addItemListener(new ItemListener() {
					public void itemStateChanged(ItemEvent e) {
						DatabaseLogin.this.cbMethods.dbTypesCBOnItemStateChange(e, DatabaseLogin.this.centerPane);
					}
				});
				GridBagConstraints gbc_dbTypesCB = new GridBagConstraints();
				gbc_dbTypesCB.fill = GridBagConstraints.BOTH;
				gbc_dbTypesCB.insets = new Insets(0, 0, 5, 0);
				gbc_dbTypesCB.gridx = 1;
				gbc_dbTypesCB.gridy = 0;
				selectPane.add(this.dbTypesCB, gbc_dbTypesCB);
			}
			{
				JSeparator separator = new JSeparator();
				GridBagConstraints gbc_separator = new GridBagConstraints();
				gbc_separator.weightx = 1.0;
				gbc_separator.insets = new Insets(0, 0, 5, 0);
				gbc_separator.fill = GridBagConstraints.BOTH;
				gbc_separator.gridwidth = 2;
				gbc_separator.gridx = 0;
				gbc_separator.gridy = 1;
				selectPane.add(separator, gbc_separator);
			}
		}
		{
			this.centerPane = new JPanel();
			this.contentPanel.add(this.centerPane, BorderLayout.CENTER);
			this.centerPane.setLayout(new CardLayout(0, 0));
			{
				this.serverPane = new ServerPanel();
				this.centerPane.add(this.serverPane, "serverPane");
			}
			{
				this.localFilePane = new LocalFilePanel();
				this.centerPane.add(this.localFilePane, "localFilePane");
			}
		}
		{
			JPanel btnPane = new JPanel();
			this.contentPanel.add(btnPane, BorderLayout.SOUTH);
			GridBagLayout gbl_btnPane = new GridBagLayout();
			gbl_btnPane.columnWidths = new int[]{127, 63, 67, 0};
			gbl_btnPane.rowHeights = new int[]{25, 2, 0};
			gbl_btnPane.columnWeights = new double[]{1.0, 0.0, 0.0, Double.MIN_VALUE};
			gbl_btnPane.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
			btnPane.setLayout(gbl_btnPane);
			{
				JSeparator separator = new JSeparator();
				GridBagConstraints gbc_separator = new GridBagConstraints();
				gbc_separator.fill = GridBagConstraints.HORIZONTAL;
				gbc_separator.insets = new Insets(0, 0, 5, 0);
				gbc_separator.gridwidth = 3;
				gbc_separator.gridx = 0;
				gbc_separator.gridy = 0;
				btnPane.add(separator, gbc_separator);
			}
			{
				JLabel label = new JLabel("");
				GridBagConstraints gbc_label = new GridBagConstraints();
				gbc_label.fill = GridBagConstraints.BOTH;
				gbc_label.insets = new Insets(0, 0, 0, 5);
				gbc_label.gridx = 0;
				gbc_label.gridy = 1;
				btnPane.add(label, gbc_label);
			}
			{
				JButton btnLogin = new JButton("Log in");
				btnLogin.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if(DatabaseLogin.this.localFilePane.isVisible()){
							DatabaseLogin.this.bMethods.btnLogInOnActionPerformed(e, DatabaseLogin.this.localFilePane);
						}else{
							DatabaseLogin.this.bMethods.btnLogInOnActionPerformed(e, DatabaseLogin.this.serverPane);
						}
					}
				});
				GridBagConstraints gbc_btnLogin = new GridBagConstraints();
				gbc_btnLogin.anchor = GridBagConstraints.EAST;
				gbc_btnLogin.fill = GridBagConstraints.VERTICAL;
				gbc_btnLogin.insets = new Insets(0, 0, 0, 5);
				gbc_btnLogin.gridx = 1;
				gbc_btnLogin.gridy = 1;
				btnPane.add(btnLogin, gbc_btnLogin);
			}
			{
				JButton btnCancel = new JButton("Cancel");
				btnCancel.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						DatabaseLogin.this.bMethods.btnCancelOnActionPerformed(e);
					}
				});
				GridBagConstraints gbc_btnCancel = new GridBagConstraints();
				gbc_btnCancel.fill = GridBagConstraints.BOTH;
				gbc_btnCancel.gridx = 2;
				gbc_btnCancel.gridy = 1;
				btnPane.add(btnCancel, gbc_btnCancel);
			}
		}
	}
	
	public void setDatabases(ArrayList<DatabaseDrivers> list)
	{
		Iterator<DatabaseDrivers> iter = list.iterator();
		while(iter.hasNext()){
			this.dbTypesCB.addItem((DatabaseDrivers)iter.next());
		}
	}
	
}
